window.onload = function (ev) {
    this.count = 0;
};
/**
 * 系统管理--车辆管理的单例对象
 */
var collection = {
    id: "collectionTable", //表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    condition:null
};
var x = 0; //传递数据专用
var y = 0;
var xnum = new Array(); //传递星星专用
var ynum = new Array();
var count = 0; //强制return专用

/**
 * 初始化表格的列
 */
collection.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {
            title: '序号', field: 'number', width: 1, align: 'center', valign: 'middle', formatter: function (value, row, index) {
                return index + 1;
            }
        },
        {title: '类型', field: 'carType', align: 'center', valign: 'middle', sortable: true},
        {title: '颜色', field: 'carColor', align: 'center', valign: 'middle', sortable: true},
        {title: '价格', field: 'carPrice', align: 'center', valign: 'middle', sortable: true},
        {title: '里程(km)', field: 'carMileage', align: 'center', valign: 'middle', sortable: true},
        {title: '金额(元/天)', field: 'rentMoneyOneDay', align: 'center', valign: 'middle', sortable: true},
        {title: '维修记录', field: 'modifyId', align: 'center', valign: 'middle', sortable: true, formatter: function (value, row, index) {
                if (row.modifyId === null || row.modifyId === undefined) {
                    return '无维修记录';
                } else{
                    return '<button type="button" class="btn btn-xs btn-success" onclick="car.lookModifyById(\''+row.modifyId+'\')">查看</button>';
                }
            }
        },
        {title: '罚单记录', field: 'ticketId', align: 'center', valign: 'middle', sortable: true, formatter: function (value, row, index) {
                if (row.ticketId === null || row.ticketId === undefined) {
                    return '无罚单记录';
                } else{
                    return '<button type="button" class="btn btn-xs btn-success" onclick="car.lookTicketById(\''+row.ticketId+'\')">查看</button>';
                }
            }
        },
        {title: '租赁次数', field: 'rentTimes', align: 'center', valign: 'middle', sortable: true},
        {title: '评分', field: 'carScore', align: 'center', valign: 'middle', sortable: true, formatter: function(value, row, index){
                xnum[index] = (index+1);
                ynum[index] = row.carScore;
                return '<div id="carScore'+(index+1)+'" name="carScore'+(index+1)+'">' +
                    '<script>$.proxy(collection.toImplementStar(xnum,ynum),window)</script>' +
                    '</div>'
            }
        },
        {title: '租赁', field: 'carId', sortable: true, align: 'center', valign: 'middle', formatter: function (value, row, index) {
                return '<button type="button" class="btn btn-xs btn-success" onclick="collection.toRentById(\''+ row.carId +'\')">租车</button>'
            }
        }
    ];
};

/**
 * 显示用户评价星际
 * @param index
 * @param number
 */
collection.toStar = function(index, number){
        if (number === null || number === "" || number === undefined){
            layui.use('rate', function () {
                var rate = layui.rate;
                var ins1 = rate.render({
                    elem: '#carScore'+(index)
                    , length: 5
                    , value: 3
                    , theme: '#909904'
                    , half: true
                    , text: true
                    , readonly: true
                });
            })
        } else{
            layui.use('rate', function () {
                var rate = layui.rate;
                var ins1 = rate.render({
                    elem: '#carScore'+(index)
                    , length: 5
                    , value: number
                    , theme: '#909904'
                    , half: true
                    , text: true
                    , readonly: true
                });
        })}
};

/**
 * 通过数据传递数据调星星内容
 * @param index
 * @param number
 */
collection.toImplementStar = function(index, number){
    if(count !== 0){
        return;
    }
    count++;
    for (var i = 0 ; i < index.length ; i++)  {
        x = index[i];
        y = number[i];
        car.toStar(x,y);
    }
};


$(function () {

    collection.app = new Vue({
        el: '#collectionCarPage',
        data: collection.condition
    });
    var defaultColunms = collection.initColumn();
    var table = new BSTable("collectionTable", "/car/myCollectionCar", defaultColunms);
    table.setPaginationType("client");
    table
    collection.table = table.init();
    laydate.render({
        elem: '#timeLimit',
        range: true,
        theme: '#009efb',
        max: Feng.currentDate()
    });
});

/**
 * 点击租车按钮
 * @param carId
 */
collection.toRentById = function (carId) {
    if(!carId){
        if (!this.check()) {
            return;
        }
    }
    var selectId = carId;
    if (!carId) {
        selectId = this.seItem.carId;
    }
    this.layerIndex = layer.open({
        type: 2,
        title: '租车中',
        area: ['800px', '500px'],
        fix: false,
        maxmin: true,
        content: Feng.ctxPath + '/car/car_rent?carId=' + selectId
    });
};